import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store/index";

Vue.use(VueRouter);

const routes = [
  // 页面端
  {
    path: "/",
    name: "Web",
    component: () => import("@/views/Web/web.vue"),
  },
  {
    path: "/web/articleInfo",
    name: "articleInfo",
    component: () => import("@/views/Web/articleInfo.vue"), 
  },


  // 管理端
  {
    redirect: "/admin/users/personal",
    path: "/admin",
    name: "Admin",
    component: () => import("@/views/Home/home.vue"),
    children: [
      {
        path: "/admin/users/personal",
        name: "UserPersonal",
        component: () => import("@/views/User/personal.vue"),
        meta: {
          title: "个人资料",
        },
      },
      {
        path: "/admin/users/changePersonal",
        name: "changePersonal",
        component: () => import("@/views/User/changePersonal.vue"),
        meta: {
          title: "修改个人资料",
        },
      },
      {
        path: "/admin/users/password",
        name: "UserPassword",
        component: () => import("@/views/User/password.vue"),
        meta: {
          title: "修改密码",
        },
      },
      {
        path: "/admin/article",
        name: "AdminArticle",
        component: () => import("@/views/Artile/artilelist.vue"),
        meta: {
          title: "文章列表",
        },
      },
      {
        path: "/admin/article/add",
        name: "ArticleAdd",
        component: () => import("@/views/Artile/addArtile.vue"),
        meta: {
          title: "发布文章",
        },
      },
      {
        path: "/admin/article/update",
        name: "ArticleUpdate",
        component: () => import("@/views/Artile/updateArtile.vue"),
        meta: {
          title: "修改文章",
        },
      },
      {
        path: "/admin/comment",
        name: "AdminComment",
        component: () => import("@/views/comment/comment.vue"),
        meta: {
          title: "评论管理",
        },
      },
      {
        path: "/admin/fans",
        name: "AdminFans",
        component: () => import("@/views/Fans/fans.vue"),
        meta: {
          title: "粉丝管理",
        },
      },
      {
        path: "/admin/star",
        name: "AdminStar",
        component: () => import("@/views/Star/star.vue"),
        meta: {
          title: "点赞管理",
        },
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/Login/login.vue"),
  },
  {
    path: "/reg",
    name: "Reg",
    component: () => import("@/views/Login/reg.vue"),
  },
];

const router = new VueRouter({
  routes,
});

// 路由守卫 前置钩子
router.beforeEach((to, from, next) => {
  // 如果用户是访问登录页面 直接放行
  if (to.path === "/login" || to.path === "/reg" || to.path === "/" || to.path ==='/web/articleInfo')
    return next();
  //获取token值
  const tokenstr = store.state.token;
  if (!tokenstr) {
    setTimeout(() => {
      alert("请先登录");
      return next("/login");
    }, 500);
  } else {
    next();
  }
});

export default router;
